package org.xfy.base.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.xfy.base.mapper.BaseMapper;
import org.xfy.base.query.BaseQuery;
import org.xfy.base.service.IBaseService;
import org.xfy.base.util.PageList;

import java.util.List;

public abstract class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Override
    public void add(T t) {
        baseMapper.add(t);
    }

    @Transactional
    @Override
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.findOne(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Transactional
    @Override
    public void patchDel(Long[] ids) {
        baseMapper.patchDel(ids);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //先查询总数量
        Long count = baseMapper.queryCount(query);
        if(count < 1){//在没有数据的情况下可以提高查询效率
            return new PageList<T>();
        }
        //再查询当前页的分页数据
        List<T> list = baseMapper.queryPage(query);
        PageList<T> pageList = new PageList<>();
        pageList.setData(list);
        pageList.setTotals(count);
        //返回查询结果
        return pageList;
    }
}